package com.sms.homework;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;


public class Homework {
	
	private int taskID;
	private int grade;//作业级别j
	private String title;
	private String tasktype;
	private String content;
	private String fromID;
	private String name;
	private String toID;	
	private String startTime;
	private String endTime;
	private String status;
	private String isAppend;	
	private List<Map> appendFile = null;//作业附件
//初始化对象
	
	public void init(Map map) {
		this.setTaskID((Integer)map.get("taskid"));
		this.setGrade((Integer)map.get("level"));
		this.setTitle((String)map.get("taskname"));
		this.setTasktype((String)map.get("task_type"));
		this.setContent((String)map.get("content"));
		this.setFromID((String)map.get("fromid"));
		this.setToID((String)map.get("toid"));	
		this.setName((String)map.get("name"));
		this.setStartTime(map.get("starttime").toString());
		this.setEndTime(map.get("endtime").toString());
		this.setStatus((String)map.get("status"));		
		this.setIsAppend((String)map.get("isappend"));
		if(isAppend.equals("1")) {
			this.setAppendFile((Integer)map.get("taskid"));
		}
		
}
	
	//获取指定文件的附件并设置
		public void setAppendFile(int fileID) {
			String sql = "select * from appendinfo where (append_type = '个人任务' or append_type = '作业结果' )" +
					"and fileid = " + fileID;
			
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			if(list.size() > 0) {//有附件时才初始化appendPath对象
				appendFile = new ArrayList<Map>();
				for(int i=0; i<list.size(); i++) {
					Map map = list.get(i);
					String type=(String)map.get("append_type");
					String fileName = (String)map.get("filename");
					String saveName = (String)map.get("savename");
					Map<String, String> map2 = new HashMap<String, String>();
					map2.put("type", type);
					map2.put("fileName", fileName);
					map2.put("saveName", saveName);
					appendFile.add(map2);
				}
			}
		}
	
		
		//学生返回指定id的作业详情
		public static List<Map> stugetwork(int taskid){
			String sql1="select * from taskinfo,allpeople where fromid=mno and taskid='"+taskid+"'";
			List<Map> list1 = new ArrayList<Map>();
			list1 = OperateDB.executeQuery(sql1);
			Map map=list1.get(0);
			if(map.get("isappend").equals('1')){
				String sql="select * from taskinfo ,allpeople,appendinfo where fromid=mno and task_type = append_type and taskid=fileid and taskid='"+taskid+"'";
				List<Map> list = new ArrayList<Map>();
				list = OperateDB.executeQuery(sql);
				list1=list;
			}
			return list1;	
							
			
		}
	
		//老师返回指定id的作业详情
	public static List<Map> getwork(int taskid){
		String sql1="select * from taskinfo,allpeople where toid=mno and taskid='"+taskid+"'";
		List<Map> list1 = new ArrayList<Map>();
		list1 = OperateDB.executeQuery(sql1);
		Map map=list1.get(0);
		if(map.get("isappend").equals('1')){
		String sql="select * from taskinfo ,allpeople,appendinfo where toid=mno and task_type = append_type and taskid=fileid and taskid='"+taskid+"'";
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
			list1=list;
		}
		return list1;	
	}
	
	//学生查看自己要做的作业
		public static List<Map> getselfhomeworks(String userno) {
			String sql="select * from taskinfo ,allpeople where fromid=mno and task_type = '个人任务'and toid='"+userno+"' order by starttime desc" ;
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			return list;		
		}
		
//搜索	
	public static List<Map> getselfhomeworks(String userno,char status) {
			String sql="select * from taskinfo ,allpeople where fromid=mno and status="+status+"  and task_type = '个人任务'and toid='"+userno+"' order by starttime desc" ;
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			return list;		
		}
	
		//查看自己所布置了的作业	
		public static List<Map> getorderedhomeworks(String userno) {	
			
			String sql="select * from taskinfo ,allpeople where fromid="+userno+" and task_type = '个人任务' and toid=mno  order by starttime desc" ;
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			return list;	
		}
		//搜索查看自己所布置了的作业			
        public static List<Map> getorderedhomeworks(String userno,char status) {	
			
			String sql="select * from taskinfo ,allpeople where fromid="+userno+" and status="+status+" and task_type = '个人任务' and toid=mno  order by starttime desc" ;
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			return list;	
		}
	
	//老师查看其他人布置了的作业
	public static List<Map> gethomeworks() {
		String sql="select * from taskinfo ,allpeople,stuinfo where fromid=sno and toid=mno and task_type = '个人任务' " ;
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		return list;		
	}
	
	//搜索 
	public static List<Map> gethomeworks(char status) {
		String sql="select * from taskinfo ,allpeople,stuinfo where fromid=sno and status="+status+"  and toid=mno and task_type = '个人任务' " ;
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		return list;		
	}
	
	//确定学生完成作业
	public static boolean changestatus(int taskid) {
		boolean isSuccess = true;
		
		String sql = "update taskinfo set status = '1' where taskid = " + taskid;
		//System.out.println(sql);
		int n = OperateDB.update(sql);
		if (n <= 0) {
			isSuccess = false;
		}
		
		return isSuccess;
	}
	
	//删除指定id的作业
		public static boolean deletework(String[] delID) {
			boolean isSuccess = true;
			if (delID.length < 1) {
				isSuccess = false;
			} else {
				Connection conn = ConnectDB.getConnection();
				boolean autoCommit;
				try {
					autoCommit = conn.getAutoCommit();
					conn.setAutoCommit(false);// 取消自动提交

					for (int i = 0; i < delID.length; i++) {
						int taskid = Integer.parseInt(delID[i]);

						String sql = "select * from taskinfo where taskid = "
								+ taskid;
						System.out.println(sql);
						List<Map> list = new ArrayList<Map>();
						list = OperateDB.executeQuery(sql);
						if (list.size() > 0) {// 存在该项目
							Map map = list.get(0);						
							// 删除appendinfo中的信息
							String isAppend = map.get("isappend").toString();
							if (isAppend.equals("1")) {// 有附件,则删除附件
								sql = "delete from appendinfo where append_type = '个人任务'"
										+ "and fileid = " + taskid;
								//System.out.println(sql);
								int n = OperateDB.update(sql);
								if (n == 0) {// 删除附件失败
									isSuccess = false;
									break; // 跳出循环
								}
							}

							// 删除taskinfo中的信息
							sql = "delete from taskinfo where taskid = " + taskid;
							//System.out.println(sql);
							int n = OperateDB.update(sql);
							if (n == 0) {// 删除失败
								isSuccess = false;
								break; // 跳出循环
							}

						} else {// 不存在项目
							isSuccess = false;
							break; // 跳出循环
						}
					}

					if (isSuccess != false) {
						conn.commit();// 手动提交
					} else {
						conn.rollback();
					}
					conn.setAutoCommit(autoCommit);// 恢复现场
				} catch (SQLException e) {
					e.printStackTrace();
					isSuccess = false;
				} catch (NumberFormatException e) {
					e.printStackTrace();
					isSuccess = false;
				} finally {
					OperateDB.close(conn);
				}
			}

			return isSuccess;
		}

		
		//提交作业
		public static boolean hand(int taskid,List<Map> appendFile){
			boolean isSuccess = true;

			Connection conn = ConnectDB.getConnection();
			boolean autoCommit;
			try {
				autoCommit = conn.getAutoCommit();
				conn.setAutoCommit(false);// 取消自动提交
               	// 有附件
							for (int i=0; i<appendFile.size(); i++) {
								Map<String, String> map = new HashMap<String, String>();
								map = (Map<String, String>)appendFile.get(i);
								String fileName = map.get("fileName").toString();
								String saveName = map.get("saveName").toString();
								// 向appendinfo表插入数据
							String	sql = "insert into appendinfo values('作业结果', ?, ?, ?)";
								List<Object> alist = new ArrayList<Object>();
								alist.add(taskid);
								alist.add(fileName);
								alist.add(saveName);
								
								OperateDB.setList(alist);
								if (OperateDB.insert(sql)) {// 插入附件表是否成功
									sql = "update taskinfo set isappend = '1' where taskid = "
											+ taskid;
									if (OperateDB.update(sql) > 0) {
										isSuccess = true;
									} else {
										isSuccess = false;
									}
								} else {
									isSuccess = false;
								}
							}// for结束
								
						
				
					if (isSuccess) {
						conn.commit();// 手动提交
					} else {
						conn.rollback();
					}
					conn.setAutoCommit(autoCommit);// 恢复现场
				} catch (SQLException e) {
					e.printStackTrace();
					isSuccess = false;
				} finally {
					OperateDB.close(conn);
				}

				return isSuccess;
			}
		

	//发布作业后插入数据库并返回 boolean值
	public static boolean inData(String fromID,String title, String endtime,
			List<String> member, String content,List<Map> appendFile) {
		String sql = "";
		System.out.print(member.toString());

		boolean isSuccess = true;

		Connection conn = ConnectDB.getConnection();
		boolean autoCommit;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			for (int j = 0; j < member.size(); j++) {
				String memberID = member.get(j);
			// 向taskinfo表插入数据
			sql = "insert into taskinfo values(null, 2, ?, '个人任务', ?, ?, ?, now(), ?, '0', '0')";
			List<Object> tlist = new ArrayList<Object>();
		
			tlist.add(title);
			tlist.add(content);
			tlist.add(fromID);
			tlist.add(memberID);
			tlist.add(endtime);
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);

				// System.out.println(tn);
				if (tn == 0) { // 插入publicinfo不成功
					isSuccess = false;
				} else {
					if (appendFile == null || appendFile.size() < 1) {// 没有添加附件时
						//System.out.println("没有附件");
						isSuccess = true;
					} else {// 有附件
						for (int i=0; i<appendFile.size(); i++) {
							Map<String, String> map = new HashMap<String, String>();
							map = (Map<String, String>)appendFile.get(i);
							String fileName = map.get("fileName").toString();
							String saveName = map.get("saveName").toString();
							// 向appendinfo表插入数据
							sql = "insert into appendinfo values('个人任务', ?, ?, ?)";
							List<Object> alist = new ArrayList<Object>();
							alist.add(tn);
							alist.add(fileName);
							alist.add(saveName);
							
							OperateDB.setList(alist);
							if (OperateDB.insert(sql)) {// 插入附件表是否成功
								sql = "update taskinfo set isappend = '1' where taskid = "
										+ tn;
								if (OperateDB.update(sql) > 0) {
									isSuccess = true;
								} else {
									isSuccess = false;
								}
							} else {
								isSuccess = false;
							}
						}// for结束
							}
					}
			}
				if (isSuccess) {
					conn.commit();// 手动提交
				} else {
					conn.rollback();
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
				isSuccess = false;
			} finally {
				OperateDB.close(conn);
			}

			return isSuccess;
		}
	
	
	
	public int getTaskID() {
		return taskID;
	}

	public void setTaskID(int taskID) {
		this.taskID = taskID;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTasktype() {
		return tasktype;
	}

	public void setTasktype(String tasktype) {
		this.tasktype = tasktype;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getFromID() {
		return fromID;
	}

	public void setFromID(String fromID) {
		this.fromID = fromID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getToID() {
		return toID;
	}

	public void setToID(String toID) {
		this.toID = toID;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getIsAppend() {
		return isAppend;
	}

	public void setIsAppend(String isAppend) {
		this.isAppend = isAppend;
	}

	public List<Map> getAppendFile() {
		return appendFile;
	}

	

}
